package cn.it.hbtt.petoa.dao;

import cn.it.hbtt.petoa.entity.RowMapper;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @author John
 * @create 2020-06-04 11:10
 */
public class BaseDAO<T> {
    //驱动
    private static String driver=null;
    //url
    private static  String url = null;
    //数据库连接账号
    private static  String user = null;
    //数据库连接密码
    private static  String pwd = null;


    static {
        try {
            Properties prop = new Properties();
            InputStream resourceAsStream = BaseDAO.class.getClassLoader().getResourceAsStream("jdbc.properties");
            prop.load(resourceAsStream);
            driver = prop.getProperty("driver");
            url= prop.getProperty("url");
            user=prop.getProperty("user");
            pwd= prop.getProperty("pwd");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取数据库连接
     *
     * @return 返回一个Connection
     */
    public static Connection getConnection() {

        try {
            Class.forName(driver);
            Connection conn = DriverManager.getConnection(url, user, pwd);
            return conn;
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return null;
    }


    /**
     * 关闭数据库连接
     *
     * @param conn 数据库连接对象
     * @param stm  Statement对象
     * @param rs   结果集对象
     */
    public static void closeAll(Connection conn, Statement stm, ResultSet rs) {
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(stm!=null){
            try {
                stm.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
    //查看通用的方法
    public List<T> commonSelect(String sql, RowMapper<T> rowMapper, Object... obj){
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        List<T> list = new ArrayList<T>();
        try {
            conn = BaseDAO.getConnection();
            PreparedStatement pstmt = conn.prepareStatement(sql);
            if(obj!=null){
                for(int i=0;i<obj.length;i++){
                    pstmt.setObject(i+1,obj[i]);
                }
            }
            rs=pstmt.executeQuery();
            while(rs.next()){
                T t = (T) rowMapper.getRow(rs);
                list.add(t);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 通用的添加、删除、修改方法
     *
     * @param sql    传入的sql语句
     * @param obj sql语句中的参数
     * @return
     */
    public static int commonIDU(String sql, Object... obj) {
        Connection conn = null;
        PreparedStatement pstmt =null;
        try {
            conn = BaseDAO.getConnection();
            pstmt = conn.prepareStatement(sql);
            for (int i = 0; i < obj.length; i++) {
                pstmt.setObject(i + 1, obj[i]);
            }
            int i = pstmt.executeUpdate();
            return i;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDAO.closeAll(conn,pstmt,null);
        }
        return 0;
    }

}